from screen import Screen
import pygame
from snake import DIR_SNAKE_UP
from snake import DIR_SNAKE_DOWN
from snake import DIR_SNAKE_LEFT
from snake import DIR_SNAKE_RIGHT

LOOP_SPEED = 10
SCREEN_SIZE_X = 800
SCREEN_SIZE_Y = 600
INIT_GRID_SIZE = 10 # 10个像素为一个格子

def defalut_option():
    return {pygame.K_w: DIR_SNAKE_UP,
     pygame.K_s: DIR_SNAKE_DOWN,
     pygame.K_a: DIR_SNAKE_LEFT,
     pygame.K_d: DIR_SNAKE_RIGHT,
     pygame.K_UP: DIR_SNAKE_UP,
     pygame.K_DOWN: DIR_SNAKE_DOWN,
     pygame.K_LEFT: DIR_SNAKE_LEFT,
     pygame.K_RIGHT: DIR_SNAKE_RIGHT}
    


DEFAULT_OPTION = defalut_option()
OPTION = DEFAULT_OPTION


def main():
    pygame.init()
    pygame_screen = pygame.display.set_mode((SCREEN_SIZE_X, SCREEN_SIZE_Y))
    pygame.display.set_caption('贪吃蛇')
    screen = Screen(SCREEN_SIZE_X, SCREEN_SIZE_Y, INIT_GRID_SIZE)
    running = True
    loop_speed = LOOP_SPEED
    while running:

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            elif event.type == pygame.KEYDOWN:
                try:
                    change_dir = OPTION[event.key]
                    screen.snake.change_dir(change_dir)
                except Exception:
                    pass
                

        pygame_screen.fill((152, 222, 163))
        screen.chk_eat()
        screen.snake.auto_move()
        screen.draw(pygame_screen)
        pygame.display.flip()        
        pygame.time.delay(loop_speed)


if __name__ == '__main__':
    main()


